//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CCanvas.cpp>
///	@path	~/src/graphics/canvas/
///	@date	2007/11/10
///	@desc	Canvas implementation.

#include "config/config.h"

#include "graphics/DxMath.h"
#include "graphics/IRenderer.h"
#include "graphics/canvas/CCanvas.h"

namespace xeres {

	IMPL_IOBJECT_CLASS( CCanvas );

	// ctor
	CCanvas::CCanvas( void )
	{
	}

	// dtor
	CCanvas::~CCanvas( void )
	{
	}

	// render all elements
	void CCanvas::Render( RenderContext& rc )
	{
		if( m_elements.empty() )
			return;

		// begin
		m_sprite->Begin( rc );

		//rc.GetDX9()->SetRenderState( D3DRS_CULLMODE  , D3DCULL_NONE );

		// for all elements
		for( size_t i = 0 , total = m_elements.size() ; i < total ; ++i )
		{
			m_elements[i]->Render( rc , m_sprite );
		}

		// end
		m_sprite->End( rc );
	}

	void CCanvas::Initialize( RefWeak<IRenderer> renderer )
	{
		m_sprite = renderer->CreateSprite();
	}

	void CCanvas::Finalize( void )
	{
		m_sprite = NULL;
	}

	// AddElement
	void CCanvas::AddElement( RefWeak<ICanvasElement> element )
	{
		m_elements.push_back( element );
	}

} // namespace xeres
